<?php

namespace plugin\filesystem\app\admin\controller;

use plugin\admin\app\model\Option;
use plugin\filesystem\api\Config;
use plugin\filesystem\api\Storage;
use support\Request;
use support\Response;
use function view;

/**
 * 邮件设置
 */
class SettingController
{
    /**
     * 邮件设置页
     * @return Response
     */
    public function index()
    {
        return view('setting/index');
    }

    /**
     * 获取设置
     * @return Response
     */
    public function get(): Response
    {
        $name = Config::SETTING_OPTION_NAME;
        $setting = Option::where('name', $name)->value('value');
        $setting = $setting ? json_decode($setting, true) : Config::getDefaultConfig();
        return json(['code' => 0, 'msg' => 'ok', 'data' => $setting]);
    }

    /**
     * 更改设置
     * @param Request $request
     * @return Response
     */
    public function save(Request $request): Response
    {
        $data = [
            'default' => $request->post('default'),
            'max_size' => $request->post('max_size'),
            'ext_yes' => $request->post('ext_yes'),
            'ext_no' => $request->post('ext_no'),
        ];
        return $this->toSave($data);
    }

    /**
     * 保存本地开发存储
     * @param Request $request
     * @return Response
     */
    public function savePublic(Request $request){
        $data = [
            'storage' => [
                'public' => [
                    'path' => $request->post('path'),
                    'url' => $request->post('url'),
                ]
            ]
        ];
        return $this->toSave($data);
    }
    /**
     * 保存本地开发存储
     * @param Request $request
     * @return Response
     */
    public function saveLocal(Request $request){
        $data = [
            'storage' => [
                'local' => [
                    'path' => $request->post('path'),
                    'url' => $request->post('url'),
                ]
            ]
        ];
        return $this->toSave($data);
    }

    /**
     * 保存阿里云
     * @param Request $request
     * @return Response
     */
    public function saveOss(Request $request): Response
    {
        $data = [
            'storage' => [
                'oss'=>[
                    'access_id'=> $request->post('access_id'),
                    'access_key' => $request->post('access_key'),
                    'bucket' => $request->post('bucket'),
                    'endpoint' => $request->post('endpoint'),
                    'ssl' => $request->post('ssl'),
                    'isCName' => $request->post('isCName'),
                    'url'=> $request->post('url')
                ]
            ]
        ];
        return $this->toSave($data,true);
    }

    /**
     * 保存腾讯云
     * @param Request $request
     * @return Response
     */
    public function saveCos(Request $request): Response
    {
        $data = [
            'storage' => [
                'cos'=>[
                    'region'=> $request->post('region'),
                    'app_id' => $request->post('app_id'),
                    'secret_id' => $request->post('secret_id'),
                    'secret_key' => $request->post('secret_key'),
                    'bucket' => $request->post('bucket'),
                    'signed_url' => $request->post('signed_url',false),
                    'read_from_cdn' => $request->post('read_from_cdn',false),
                    'url'=> $request->post('url')
                ]
            ]
        ];
        return $this->toSave($data,true);
    }

    /**
     * 保存七牛云
     * @param Request $request
     * @return Response
     */
    public function saveQiniu(Request $request): Response
    {
        $data = [
            'storage' => [
                'qiniu'=>[
                    'accessKey'=> $request->post('accessKey'),
                    'secretKey' => $request->post('secretKey'),
                    'bucket' => $request->post('bucket'),
                    'domain' => $request->post('domain'),
                    'url'=> $request->post('url')
                ]
            ]
        ];
        return $this->toSave($data,true);
    }

    /**
     * 保存ftp
     * @param Request $request
     * @return Response
     */
    public function saveFtp(Request $request): Response
    {
        $data = [
            'storage' => [
                'ftp'=>[
                    'host'=> $request->post('host'),
                    'username' => $request->post('username'),
                    'password' => $request->post('password'),
                    'port' => $request->post('port',21),
                    'root' => $request->post('root'),
                    'passive' => $request->post('passive',false),
                    'ssl' => $request->post('ssl',false),
                    'url'=> $request->post('url')
                ]
            ]
        ];
        return $this->toSave($data,true);
    }

    /**
     * 保存S3
     * @param Request $request
     * @return Response
     */
    public function saveS3(Request $request): Response
    {
        $data = [
            'storage' => [
                's3'=>[
                    'key' => $request->post('key'),
                    'secret' => $request->post('secret'),
                    'region' => $request->post('region'),
                    'version' => $request->post('version'),
                    'bucket_endpoint' => $request->post('bucket_endpoint',false),
                    'use_path_style_endpoint' => $request->post('use_path_style_endpoint',false),
                    'endpoint' => $request->post('endpoint'),
                    'bucket_name' => $request->post('bucket_name'),
                    'url'=> $request->post('url')
                ]
            ]
        ];
        return $this->toSave($data,true);
    }

    public function upload(Request $request){
        $file = $request->file('file');
        $result = Storage::upload($file);
        return json([
            'code' => 0,
            'msg' => '上传成功',
            'data' => $result
        ]);
    }

    /**
     * 写入数据
     * @param $data
     * @return Response
     */
    private function toSave($data,$storage = false){
        $name = Config::SETTING_OPTION_NAME;
        $option = Option::where('name', $name)->first();
        if ($option) {
            $values = \json_decode($option->value,true);
            if($storage){
                $values['storage'] = array_merge($values['storage'],$data['storage']);
            }else{
                $values = array_merge($values,$data);
            }

            Option::where('name', $name)->update(['value' => json_encode($values)]);
        } else {
            $values = Config::getDefaultConfig();
            if($storage){
                $values['storage'] = array_merge($values['storage'],$data['storage']);
            }else{
                $values = array_merge($values,$data);
            }
            $option = new Option();
            $option->name = $name;
            $option->value = json_encode($values);
            $option->save();
        }
        return json(['code' => 0, 'msg' => 'ok']);
    }



}
